import http from './interface'
import store from '@/store'
//设置请求前拦截器
http.interceptor.request = (config) => {
	config.header = {
		'token': store.state.userInfo.token,
	}

}
//设置请求结束后拦截器
http.interceptor.response = (response) => {
	console.log("=============--------"+store.state.userInfo.token)
	uni.hideLoading()
	if(response.statusCode === 200){
		if ( response.data.code === 20001 || response.data.code === 20002) {
			//未登录
			uni.showToast({
				title: response.data.message,
				duration: 1200,
				icon: 'none'
			})
			setTimeout(function() {
				uni.reLaunch({
					url: '/pages/mine/author',
					animationType: 'slide-in-bottom',
					"animationDuration": 450
				});
			}, 450);
			store.commit('logout')
			return
		}
	}else{
		uni.showToast({
			title: '网络好像不太通畅~',
			duration: 1200,
			icon: 'none'
		})
	}

}
//微信登录信息
export const getSessionKey = (data) => {

	return http.request({
		url: 'user/getSessionKey',
		method: 'POST',
		data,
	})
}
//获取验证码
export const getVerificationCode = (data) => {

	return http.request({
		url: 'getVerificationCode',
		method: 'POST',
		data,
	})
}
//注册
export const register = (data) => {

	return http.request({
		url: 'user/register',
		method: 'POST',
		data,
	})
}
//登录
export const login = (data) => {

	return http.request({
		url: 'user/login',
		method: 'POST',
		data,
	})
}
//获取用户信息
export const getUserInfo = (data) => {

	return http.request({
		url: 'user/getUserInfo',
		method: 'POST',
		data,
	})
}
//重置密码
export const resetPassword = (data) => {

	return http.request({
		url: 'user/resetPassword',
		method: 'POST',
		data,
	})
}
export default {
	getVerificationCode,
	register,
	login,
	getUserInfo,
	resetPassword,
	getSessionKey,
}
